package com.aaa.service.sas.Impl;

import com.aaa.dao.sas.TSasRecordDetailsMapper;
import com.aaa.dao.sas.TSasSubMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.sas.TSasRecordDetails;
import com.aaa.entity.sas.TSasSub;
import com.aaa.service.sas.TSasSubService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (TSasSub)表服务实现类
 *
 * @author makejava
 * @since 2022-10-29 00:49:01
 */
@Service
public class TSasSubServiceImpl implements TSasSubService {
   @Autowired
   private TSasSubMapper tSasSubMapper;
   @Autowired
   private TSasRecordDetailsMapper tSasRecordDetailsMapper;

   @Override
   public PageInfo queryAll(MyPage page, TSasSub tSasSub) {
      PageHelper.startPage(page.getPage(),page.getLimit());
      List<TSasSub> tSasSubs = tSasSubMapper.queryAll(tSasSub);
      return new PageInfo(tSasSubs);
   }

   @Override
   public Boolean importExcel(MultipartFile file) {
      int i = tSasSubMapper.selectCount(null);
      if (i>0){
         return false;
      }else {
         InputStream is = null;
         try {
            is = file.getInputStream();
         } catch (IOException e) {
            e.printStackTrace();
         }
         EasyExcel.read(is, TSasSub.class, new AnalysisEventListener<TSasSub>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TSasSub> cachedDataList = new ArrayList<>();

            @Override
            public void invoke(TSasSub data, AnalysisContext context) {
               cachedDataList.add(data);
               if (cachedDataList.size() >= BATCH_COUNT) {
                  saveData();
                  // 存储完成清理 list
                  cachedDataList = new ArrayList<>();
               }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
               saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
//            log.info("存储数据库成功！");
               save(cachedDataList);
            }
         }).sheet("焦虑自评量表").doRead();
         return true;
      }
   }

   @Override
   public void save(List<TSasSub> list) {
      tSasSubMapper.insertSASTitle(list);
   }

   @Override
   public Boolean addOrUpdataSASTitle(TSasSub tSasSub) {
      int i = -1;
      if (tSasSub.getId()==null) {
         i = tSasSubMapper.insert(tSasSub);
      }else {
         i = tSasSubMapper.updateByPrimaryKey(tSasSub);
      }
      return i>=0?true:false;
   }

   @Override
   public Boolean delSasTitle(Integer sasnumber) {
      int i=-1;
      TSasRecordDetails tSasRecordDetails = new TSasRecordDetails();
      tSasRecordDetails.setSasnumber(sasnumber);
      int i1 = tSasRecordDetailsMapper.selectCount(tSasRecordDetails);
      System.out.println("该题目已经被多少人做 = " + i1);
      if (i1 == 0){
         TSasSub tSasSub = new TSasSub();
         tSasSub.setSasnumber(sasnumber);
         i = tSasSubMapper.delete(tSasSub);
      }
      return i>=0?true:false;
   }

   @Override
   public List<Integer> delSasnumbers(Integer[] sasnumbers) {
      List<Integer> dellist = new ArrayList();
      List<Integer> nodellist = new ArrayList();
      for (Integer sasnumber : sasnumbers) {
         TSasRecordDetails tSasRecordDetails = new TSasRecordDetails();
         tSasRecordDetails.setSasnumber(sasnumber);
         int i = tSasRecordDetailsMapper.selectCount(tSasRecordDetails);
         System.out.println(sasnumber+"题已经被"+i+"人做");
         if (i == 0){
            dellist.add(sasnumber);
         }else {
            nodellist.add(sasnumber);
         }
      }
      if (dellist.size()>0){
         int i = tSasSubMapper.delSasnumbers(dellist);
         System.out.println("删除行数为i ================== " + i);
      }else {
         System.out.println("删除行数为i ================== 0");
      }
      return nodellist;
   }

   @Override
   public Boolean checkSasNumber(Integer sasnumber) {
      TSasSub tSasSub = new TSasSub();
      tSasSub.setSasnumber(sasnumber);

      TSasSub tSasSub1 = tSasSubMapper.selectOne(tSasSub);
      return tSasSub1==null;
   }

   @Override
   public PageInfo getIndexAllSub(MyPage page) {
      PageHelper.startPage(page.getPage(),page.getLimit());

      List<TSasSub> tSasSubs = tSasSubMapper.selectAll();
      System.out.println("tSasSubs = " + tSasSubs);
      return new PageInfo(tSasSubs);
   }


}
